package com.ld.security.server.blog.modules.cms.web;


import com.ld.security.common.mybatis.base.po.EntityPo;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.mybatis.base.po.PagePo;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.mybatis.base.vo.PageVo;
import com.ld.security.common.mybatis.util.JdbcUtil;
import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.server.blog.modules.cms.entity.CmsCollectEntity;
import com.ld.security.server.blog.modules.cms.entity.CmsCommentEntity;
import com.ld.security.server.blog.modules.cms.entity.CmsContentEntity;
import com.ld.security.server.blog.modules.cms.entity.CmsTypeEntity;
import com.ld.security.server.blog.modules.cms.service.ICmsCollectService;
import com.ld.security.server.blog.modules.cms.service.ICmsCommentService;
import com.ld.security.server.blog.modules.cms.service.ICmsContentService;
import com.ld.security.server.blog.modules.cms.service.ICmsTypeService;
import com.ld.security.server.blog.modules.cms.vo.AdminVo;
import com.ld.security.server.blog.modules.cms.vo.CollectDetailsVo;
import com.ld.security.server.user.modules.rec.po.CardClickPo;
import com.ld.security.server.user.modules.rec.service.IRecCardService;
import com.ld.security.server.user.modules.ups.util.UpsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags="主页Wen管理")
@RequestMapping("/web/admin")
public class AdminWeb {

    @Autowired
    private ICmsCollectService iCmsCollectService;

    @Autowired
    ICmsContentService iCmsContentService;

    @Autowired
    ICmsCommentService iCmsCommentService;

    @Autowired
    ICmsTypeService iCmsTypeService;


    @Autowired(required = false)
    CmsDetailsEvent cmsDetailsEvent;


    @AjaxLogKey
    @ApiOperation("分页内容")
    @RequestMapping(value = "pageContent",method = RequestMethod.POST)
    public DataVo pageContent(@RequestBody @Valid PagePo<CmsContentEntity> po){
        po.getQuery().setCollectId("-1");
        PageVo<CmsContentEntity> vo =  iCmsContentService.page(po);
        for (CmsContentEntity e : vo.getList()) {
          String content =e.getDigest();
          e.setContent(content);
        }
        return DataVo.of(vo);
    }
    @AjaxLogKey(inParams = true,outParams = true)
    @ApiOperation("内容详情")
    @RequestMapping(value = "getContentDetails",method = RequestMethod.POST)
    public DataVo getContentDetails(@RequestBody @Valid IdPo po){
        CmsContentEntity cmsContentEntity =  iCmsContentService.getArticle(po.getId());


        if(cmsDetailsEvent != null){
            cmsDetailsEvent.getContentDetais(cmsContentEntity);
        }


        return DataVo.of(cmsContentEntity);
    }
    @AjaxLogKey
    @ApiOperation("分页专辑")
    @RequestMapping(value = "pageCollect",method = RequestMethod.POST)
    public DataVo pageCollect(@RequestBody @Valid PagePo<CmsCollectEntity> po){
        PageVo<CmsCollectEntity> vo =  iCmsCollectService.page(po);
        for (CmsCollectEntity e : vo.getList()) {
            String content =e.getDigest();
            e.setContent(content);
        }
        return DataVo.of(vo);
    }
    @AjaxLogKey
    @ApiOperation("专辑详情")
    @RequestMapping(value = "getCollectDetails",method = RequestMethod.POST)
    public DataVo getCollectDetails(@RequestBody @Valid IdPo po){
        CollectDetailsVo e =  iCmsCollectService.getDetails(po.getId());
        if(cmsDetailsEvent != null){
            cmsDetailsEvent.getCollectDetails(e);
        }
        return DataVo.of(e);
    }
    @AjaxLogKey
    @ApiOperation("分页评论")
    @RequestMapping(value = "pageComment",method = RequestMethod.POST)
    public DataVo pageComment(@RequestBody @Valid PagePo<CmsCommentEntity> po){
        PageVo<CmsCommentEntity> e =  iCmsCommentService.page(po);
        return DataVo.of(e);
    }
    @AjaxLogKey
    @ApiOperation("添加评论")
    @RequestMapping(value = "saveComment",method = RequestMethod.POST)
    public DataVo saveComment(@RequestBody @Valid EntityPo<CmsCommentEntity> po){
        int total =   iCmsCommentService.save(po.getEntity());
        return DataVo.ofMsg("评论发表成功");
    }


    @ApiOperation("获取所有分类")
    @RequestMapping(value = "getTypeAll",method = RequestMethod.POST)
    public DataVo getTypeAll(){
        List<CmsTypeEntity> list =  iCmsTypeService.all();
        PageVo<CmsTypeEntity> e =PageVo.of(list);
        return DataVo.of(e);
    }


    public List<CmsCollectEntity> getCollect(){
        PagePo p =  new PagePo<>();
        p.setOrderBy("clickTotal");
        p.setAsc(false);
        p.setQuery(new CmsCollectEntity());
        return  iCmsCollectService.page(p).getList();
    }


    public List<CmsContentEntity> getContent(){
        PagePo p =  new PagePo<>();

        p.setOrderBy("clickTotal");
        p.setAsc(false);
        CmsContentEntity q = new CmsContentEntity();
        q.setCollectId("-1");
        p.setQuery(q);
        return  iCmsContentService.page(p).getList();
    }


    @Autowired
    IRecCardService iRecCardService;

  @ApiOperation("卡片被点击")
  @RequestMapping(value = "cardClick", method = RequestMethod.POST)
  public DataVo cardClick(@RequestBody CardClickPo po)
  {
      iRecCardService.cardClick(po);
        return DataVo.of();
  }

        @AjaxLogKey
    @ApiOperation("首页数据")
    @RequestMapping(value = "getAdmin",method = RequestMethod.POST)
    public DataVo getAdmin(){
        AdminVo vo =new AdminVo();
        JdbcUtil.proxyQuery(vo);




        for (CmsCollectEntity cmsCollectEntity : vo.getCollects()) {
          String content = cmsCollectEntity.getDigest();
          cmsCollectEntity.setContent(content);
        }

    for (CmsContentEntity cmsContentEntity : vo.getContents()) {
        String content = cmsContentEntity.getDigest();
        cmsContentEntity.setContent(content);
    }
        return DataVo.of(vo);
    }

}
